﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class UploadImagesAjax : System.Web.UI.UserControl
    {
        ImageThumbnail imt = new ImageThumbnail();
        string strFileName = "";
        string typeFile;
        System.Drawing.Image imgPhoto = null;
        int newWidth=0;
        int newHeight=0;
        int thumWidth = 0;
        int thumHeight = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            GetNameFile();
        }

        protected string GetNameFile()
        {
            HttpPostedFile hpfFile = Request.Files["myfile"];
            String path = Server.MapPath(ConfigurationManager.AppSettings["FolderImagesUpload"]);

            if (Path.GetExtension(hpfFile.FileName.ToLower()) == ".jpg" || Path.GetExtension(hpfFile.FileName.ToLower()) == ".png" || Path.GetExtension(hpfFile.FileName.ToLower()) == ".bmp" || Path.GetExtension(hpfFile.FileName.ToLower()) == ".gif")
            {

                try
                {
                    int index = hpfFile.FileName.LastIndexOf('.');
                    typeFile = hpfFile.FileName.Substring(index);
                    strFileName = DateTime.Now.ToString("ddMMyyyyhhmmss").Trim() + typeFile;

                    hpfFile.SaveAs(path + strFileName);

                    Session[CONST.SESSION_IMG] ="larger_"+ strFileName;

                    //hpfFile.SaveAs(path + hpfFile.FileName);
                    System.Drawing.Image imgPhotoVert = System.Drawing.Image.FromFile(path + strFileName);

                    int size = int.Parse(ConfigurationManager.AppSettings["SizeImageDefault"]);
                    int large = int.Parse(ConfigurationManager.AppSettings["SizeImageLarge"]);

                    if (imgPhotoVert.Width > imgPhotoVert.Height)
                    {
                        if (imgPhotoVert.Width > 540)
                        {
                            newWidth = 540;
                            newHeight = (540 * imgPhotoVert.Height) / imgPhotoVert.Width;    
                        }                        
                    }
                    else
                    {
                        if (imgPhotoVert.Height > 540)
                        {
                            newHeight = 540;
                            newWidth = (540 * imgPhotoVert.Width) / imgPhotoVert.Height;
                        }                       
                    }

                    if(newHeight !=0 && newWidth!=0)
                    {
                        imgPhotoVert = imt.FixedSize(imgPhotoVert, newWidth, newHeight);
                    }
                    

                    imgPhoto = imt.ImagesCopyRight(imgPhotoVert, "");

                    //imgPhoto = imt.ImagesWatermark(imgPhotoVert, System.Drawing.Image.FromFile(Server.MapPath(ConfigurationManager.AppSettings["imgWatermark"].ToString())));

                    imgPhoto.Save(path + "larger_" + strFileName, ImageFormat.Jpeg);  // Lưu ảnh lớn 

                    if (imgPhoto.Width > imgPhoto.Height)
                    {
                        thumWidth = size;
                        thumHeight = (size * imgPhoto.Height) / imgPhoto.Width;                       
                    }
                    else
                    {
                        thumHeight = size;
                        thumWidth = (size * imgPhoto.Width) / imgPhoto.Height;
                    }

                    imgPhoto = imt.FixedSize(imgPhoto, thumWidth, thumHeight);
                    
                    imgPhoto.Save(path + "img_" + strFileName, ImageFormat.Jpeg);  //Lưu ảnh nhỏ 

                    Session[CONST.SESSION_IMG_THUMBNAIL] = "img_" + strFileName;

                    imgPhoto.Dispose();

                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }               
            }
            return strFileName;
        }
    }
}